import TreeCesiumData from "./layer_tree_data";
import Environment from "../../utils/environment";
import LayerManager from "@/plugins/mev/layer/LayerManager";

class ModelData extends TreeCesiumData {
    constructor(data, viewer) {
        super(data, viewer);
        this._model = null;
        this._data = data;
        this._viewer = viewer;
        this._entity = null;
        this._DataObject = null;
        /* this._target = null;*/
    }

    show() {
        let that = this;
        if (!that.properties) {
            that.properties = {};

            that.properties.lon = parseFloat(that._data.properties.lon);

            that.properties.lat = parseFloat(this._data.properties.lat);

            that.properties.height = parseFloat(this._data.properties.height);

            that.properties.scale = parseFloat(this._data.properties.scale);
        }
        let position = Cesium.Cartesian3.fromDegrees(that.properties.lon, that.properties.lat, that.properties.height);
        let heading = Cesium.Math.toRadians(0);
        let pitch = 0;
        let roll = 0;
        let hpr = new Cesium.HeadingPitchRoll(heading, pitch, roll);

        let orientation = Cesium.Transforms.headingPitchRollQuaternion(position, hpr);

        that._entity = {
            position: position,
            orientation: orientation,
            model: {
                /*uri: Environment.getDataBasePath() + "/" + this._data.path,*/
                uri: that._data.path,
                minimumPixelSize: 128,
                maximumScale: that.properties.scale
            }
        };

        if (!this._viewer.entities.contains(this._entity)) {
            this._DataObject = this._viewer.entities.add(this._entity);
          /*  this.flyTo(that._DataObject);*/
        }
    }


    close() {
        if (this._DataObject) {
            if (this._viewer.entities.contains(this._DataObject)) {
                this._viewer.entities.remove(this._DataObject);
            }
            this._DataObject = null;
        }
    }

    setProperties(props) {
        let that = this;
        that.properties.lon = parseFloat(props.lon);
        that.properties.lat = parseFloat(props.lat);
        that.properties.height = parseFloat(props.height);
        that.properties.scale = parseFloat(props.scale);
        let position = Cesium.Cartesian3.fromDegrees(that.properties.lon, that.properties.lat, that.properties.height);
        let heading = Cesium.Math.toRadians(0);
        let pitch = 0;
        let roll = 0;
        let hpr = new Cesium.HeadingPitchRoll(heading, pitch, roll);

        let orientation = Cesium.Transforms.headingPitchRollQuaternion(position, hpr);
        this._DataObject.position = position;
        this._DataObject.orientation = orientation;
        this._DataObject.model.maximumScale = that.properties.scale;
       /* that.flyTo(that._DataObject)*/
    }
}

export default ModelData;
